﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Net.Sockets;
using IPMsgExpections;
using CommonUtility;

namespace IPMSG
{
    public class IPMsgManager
    {
        private static IPMsgManager instance = new IPMsgManager();

        //用户列表字典，key为主机名
        private Dictionary<string, RemoteUser> remoteUsers = new Dictionary<string, RemoteUser>();

        //获取或设置用户列表字典
        internal Dictionary<string, RemoteUser> RemoteUsers
        {
            get { return this.remoteUsers; }
            set { this.remoteUsers = value; }
        }

        //单件模式
        public static IPMsgManager Instance
        {
            get { return instance; }
        }

        /// <summary>
        /// 通过IP地址来查找RemoteUser
        /// </summary>
        /// <param name="ipAddr">IP地址</param>
        /// <returns>RemoteUser</returns>
        internal RemoteUser GetRemoteuserByIPAddr(IPAddress ipAddr)
        {
            foreach (RemoteUser remoteUser in this.remoteUsers.Values)
            {
                if (remoteUser.IpEP.Address.Equals(ipAddr))
                {
                    return remoteUser;
                }
            }

            return null;
        }

        #region 开放给UI的公共方法

        /// <summary>
        /// 启动IPMSG
        /// </summary>
        public void Start()
        {
            UdpAdapterServer.Instance.StartUdpServer();
        }

        /// <summary>
        /// 发送文本消息
        /// </summary>
        /// <param name="hostNameTo">对方主机名（区分大小写）</param>
        /// <param name="msgToSend">要发送的文本信息</param>
        public void SendMsg(string hostNameTo, string msgToSend)
        {
            if (this.remoteUsers.ContainsKey(hostNameTo))
            {
                try
                {
                    this.remoteUsers[hostNameTo].SendTxtMsg(msgToSend);
                }
                catch (Exception ex)
                {
                    throw new IPmsgExpection("消息发送失败.", ex);
                }
            }
            else
            {
                throw new IPmsgExpection("主机名不存在，消息发送失败.");
            }
        }

        /// <summary>
        /// 发送文件
        /// </summary>
        /// <param name="p">对方主机名（区分大小写）</param>
        /// <param name="p_2">要发送的文件地址</param>
        public void SendFile(string hostNameTo, string filePath)
        {
            if (this.remoteUsers.ContainsKey(hostNameTo))
            {
                try
                {
                    this.remoteUsers[hostNameTo].SendFile(filePath);
                }
                catch (Exception ex)
                {
                    throw new IPmsgExpection("文件发送失败.", ex);
                }
            }
            else
            {
                throw new IPmsgExpection("主机名不存在，文件发送失败.");
            }
        }

        /// <summary>
        /// 刷新用户列表
        /// </summary>
        public void Refresh()
        {
            try
            {
                this.remoteUsers.Clear();
                UdpAdapterServer.Instance.BroadCastBrEntry();
            }
            catch (Exception ex)
            {
                throw new IPmsgExpection(ex.Message, ex);
            }
        }

        /// <summary>
        /// 离线
        /// </summary>
        public void Exit()
        {
            try
            {
                this.remoteUsers.Clear();
                UdpAdapterServer.Instance.BroadCastBrExit();
            }
            catch (Exception ex)
            {
                throw new IPmsgExpection(ex.Message, ex);
            }
        }

        public void ReadMsg(string hostNameTo, uint indexPack)
        {
            if (this.remoteUsers.ContainsKey(hostNameTo))
            {
                try
                {
                    this.remoteUsers[hostNameTo].NotifyReadMsg(indexPack);
                }
                catch (Exception ex)
                {
                    throw new IPmsgExpection("消息发送失败.", ex);
                }
            }
            else
            {
                throw new IPmsgExpection("主机名不存在，消息发送失败.");
            }
        }
        #endregion
    }
}
